import 'package:elephant_fish/db/hi_cache.dart';
import 'package:elephant_fish/http/core/hi_net.dart';
import 'package:elephant_fish/http/request/base_request.dart';
import 'package:elephant_fish/http/request/login_request.dart';
import 'package:elephant_fish/http/request/registar_request.dart';
import 'package:shared_preferences/shared_preferences.dart';

class LoginDao{
  static const USERTOKEN = "app-token";
  static login(String phone,{String type,String password,String code}){
    if(type == null){
      return _send(phone,code: code);
    }else if(type.compareTo('code') == 0){
      return _send(phone,code: code);
    }else{
      return _send(phone,password: password);
    }

  }

  static _send(String phone,{type,password,code}) async{
    BaseRequest request ;
    request = LoginRequest();
    request.add('phone', phone)
    .add('type', type)
    .add('password', password)
    .add('code', code)
    .add('login_type',2);
    var result = await HiNet.getInstance().fire(request);
    print(result);
    if(result['code'] == 0){
      SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
      sharedPreferences.setString(USERTOKEN, result['data']['app_token']);
    }
    return result;
  }

  static getUserToken(){
    HiCache.getInstance().getString(USERTOKEN);
  }

}